from Base.ReadExcel import Read_Excel
from Base.HttpBase import HttpBase
from Base.WriteExcel import Write_excel
from RequestService.LoginGetCookie import getCookie
import os, time, json
from Base import Log, GetYamlFile
from Test.DingTalkTest import SendText

log = Log.Log()
cookie = getCookie()

fail_result=[]
'''运行单条用例结果'''
def RequestsRun(case):
    #定义空结果
    result = {}
    # 返回cookie
    # log.info("得到cookie："+cookie)
    headers = {
        "cookie": cookie
    }
    #判断参数是否为空
    params = case["params"]
    if params.strip() == '':
        log.error(case["testName"]+"：该用例参数为空")
        params = case["params"]
    else:
        params = json.loads(params)
        log.info(case["testName"]+"---params格式:"+str(type(params)))
    # 写入自定义返回结果
    request = HttpBase(case["url"], params, case["method"], headers).RequestRun()
    # 写入row数字
    result["rowNum"] = case["rowNum"]
    # 写入测试用例名称
    result["testName"] = case["testName"]
    # 写入请求方式
    result["method"] = case["method"]
    # 写入完成的rul
    result["url"] = case["url"]
    # 写入参数
    result["params"] = case["params"]
    # 写入预期结果
    result["hopeResult"] = case["hopeResult"]
    # 写入实际结果
    result["actualResult"] = str(request.content, 'utf8')
    # 写入接口运行时间
    time = request.elapsed.total_seconds() * 1000
    time = round(time, 2)
    result["time"] = str(time)
    # 判断实际结果是否包含预期结果内容，且输出结果 pass or fail
    if result["hopeResult"] in result["actualResult"] and time <= 100:
        result["result"] = "pass"
        log.info(result["testName"] + "响应内容匹配，测试通过")
    else:
        result["result"] = "fail"
        log.error(result["testName"] + "响应内容不匹配，接口存在问题")
    get_result = str(result)
    log.info("获取测试结果:" + get_result)
    # if len(fail_result)!=0 :
    #     send_text = SendText(webhook)
    #     send_text.send_text(msg="测试结果中含有失败结果集"+str(fail_result))
    # else:
    #     send_text = SendText(webhook)
    #     send_text.send_text(msg="执行测试用例通过" )
    return result





if __name__ == '__main__':
    RequestsRun()
    # get_head_name_num=get_excel_head_name_num("Request")
    # # print(get_head_name_num)
    # x={
    #     "test":"Test"
    # }
    # print(x["rrr"])
